//
//  MMColorBoard.m
//  MemeMaker
//
//  Created by dangthan on 7/13/13.
//  Copyright (c) 2013 soyo. All rights reserved.
//

#import "MMColorBoard.h"
#import <QuartzCore/QuartzCore.h>

@interface MMColorBoard () {
    UIButton *_currentView;
}

@end

@implementation MMColorBoard
@synthesize delegate;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        self.layer.borderColor = [UIColor blackColor].CGColor;
        self.layer.borderWidth = 1.0;
    }
    return self;
}

- (id) initWithCoder:(NSCoder *)aDecoder {
    self = [super initWithCoder:aDecoder];
    if (self) {
        //init
        self.layer.borderColor = [UIColor blackColor].CGColor;
        self.layer.borderWidth = 1.0;
    }
    return self;
}

#pragma mark - custom method
- (void) configImage:(UIButton *)btn withImage:(UIImage *)img {
    [btn setImage:img forState:UIControlStateNormal];
}


#pragma mark - IBAction method
- (IBAction)btnColorClick:(id)sender {
    UIButton *button = (UIButton *)sender;
    UIColor *tmpColor = nil;
    UIImage *imgWhite = [UIImage imageNamed:@"color1.png"];
    UIImage *imgWhite_sel = [UIImage imageNamed:@"color1_sel.png"];
    UIImage *imgYellow = [UIImage imageNamed:@"color2.png"];
    UIImage *imgYellow_sel = [UIImage imageNamed:@"color2_sel.png"];
    UIImage *imgRed = [UIImage imageNamed:@"color3.png"];
    UIImage *imgRed_sel = [UIImage imageNamed:@"color3_sel.png"];
    UIImage *imgPurple = [UIImage imageNamed:@"color4.png"];
    UIImage *imgPurple_sel = [UIImage imageNamed:@"color4_sel.png"];
    UIImage *imgGreen1 = [UIImage imageNamed:@"color5.png"];
    UIImage *imgGreen1_sel = [UIImage imageNamed:@"color5_sel.png"];
    UIImage *imgGreen2 = [UIImage imageNamed:@"color6.png"];
    UIImage *imgGreen2_sel = [UIImage imageNamed:@"color6_sel.png"];
    UIImage *imgLightgray = [UIImage imageNamed:@"color7.png"];
    UIImage *imgLightgray_sel = [UIImage imageNamed:@"color7_sel.png"];
    UIImage *imgBlack = [UIImage imageNamed:@"color8.png"];
    UIImage *imgBlack_sel = [UIImage imageNamed:@"color8_sel.png"];
    UIImage *imgBar = [UIImage imageNamed:@"color9.png"];
    UIImage *imgBar_sel = [UIImage imageNamed:@"color9_sel.png"];
    
    switch (button.tag) {
        case 1: {
            tmpColor = [UIColor colorWithRed:1.0 green:10 blue:1.0 alpha:1.0];
            [btnWhite setImage:imgWhite_sel forState:UIControlStateNormal];
            [btnGreen setImage:imgGreen1 forState:UIControlStateNormal];
            [btnGreenBold setImage:imgGreen2 forState:UIControlStateNormal];
            [btnLighGray setImage:imgLightgray forState:UIControlStateNormal];
            [btnBlack setImage:imgBlack forState:UIControlStateNormal];
            [btnRed setImage:imgRed forState:UIControlStateNormal];
            [btnPurple setImage:imgPurple forState:UIControlStateNormal];
            [btnYellow setImage:imgYellow forState:UIControlStateNormal];
            [btnShowBarColor setImage:imgBar forState:UIControlStateNormal];
        }
            break;
        case 2: {
            tmpColor = [UIColor yellowColor];
            [btnWhite setImage:imgWhite forState:UIControlStateNormal];
            [btnGreen setImage:imgGreen1 forState:UIControlStateNormal];
            [btnGreenBold setImage:imgGreen2 forState:UIControlStateNormal];
            [btnLighGray setImage:imgLightgray forState:UIControlStateNormal];
            [btnBlack setImage:imgBlack forState:UIControlStateNormal];
            [btnRed setImage:imgRed forState:UIControlStateNormal];
            [btnPurple setImage:imgPurple forState:UIControlStateNormal];
            [btnYellow setImage:imgYellow_sel forState:UIControlStateNormal];
            [btnShowBarColor setImage:imgBar forState:UIControlStateNormal];
        }
            break;
        case 3: { // for Red
            tmpColor = [UIColor redColor];
            [btnWhite setImage:imgWhite forState:UIControlStateNormal];
            [btnGreen setImage:imgGreen1 forState:UIControlStateNormal];
            [btnGreenBold setImage:imgGreen2 forState:UIControlStateNormal];
            [btnLighGray setImage:imgLightgray forState:UIControlStateNormal];
            [btnBlack setImage:imgBlack forState:UIControlStateNormal];
            [btnRed setImage:imgRed_sel forState:UIControlStateNormal];
            [btnPurple setImage:imgPurple forState:UIControlStateNormal];
            [btnYellow setImage:imgYellow forState:UIControlStateNormal];
            [btnShowBarColor setImage:imgBar forState:UIControlStateNormal];
        }
            break;
        case 4: { // for purple
            tmpColor = [UIColor magentaColor];
            [btnWhite setImage:imgWhite forState:UIControlStateNormal];
            [btnGreen setImage:imgGreen1 forState:UIControlStateNormal];
            [btnGreenBold setImage:imgGreen2 forState:UIControlStateNormal];
            [btnLighGray setImage:imgLightgray forState:UIControlStateNormal];
            [btnBlack setImage:imgBlack forState:UIControlStateNormal];
            [btnRed setImage:imgRed forState:UIControlStateNormal];
            [btnPurple setImage:imgPurple_sel forState:UIControlStateNormal];
            [btnYellow setImage:imgYellow forState:UIControlStateNormal];
            [btnShowBarColor setImage:imgBar forState:UIControlStateNormal];
        }
            break;
        case 5: { //for green1
            tmpColor = [UIColor colorWithRed:64.0/255 green:159.0/255 blue:251.0/255 alpha:1.0];
            [btnWhite setImage:imgWhite forState:UIControlStateNormal];
            [btnGreen setImage:imgGreen1_sel forState:UIControlStateNormal];
            [btnGreenBold setImage:imgGreen2 forState:UIControlStateNormal];
            [btnLighGray setImage:imgLightgray forState:UIControlStateNormal];
            [btnBlack setImage:imgBlack forState:UIControlStateNormal];
            [btnRed setImage:imgRed forState:UIControlStateNormal];
            [btnPurple setImage:imgPurple forState:UIControlStateNormal];
            [btnYellow setImage:imgYellow forState:UIControlStateNormal];
            [btnShowBarColor setImage:imgBar forState:UIControlStateNormal];
        }
            break;
        case 6: { //for green2
            tmpColor = [UIColor colorWithRed:16.0/255 green:77.0/255 blue:75.0/255 alpha:1.0];
            [btnWhite setImage:imgWhite forState:UIControlStateNormal];
            [btnGreen setImage:imgGreen1 forState:UIControlStateNormal];
            [btnGreenBold setImage:imgGreen2_sel forState:UIControlStateNormal];
            [btnLighGray setImage:imgLightgray forState:UIControlStateNormal];
            [btnBlack setImage:imgBlack forState:UIControlStateNormal];
            [btnRed setImage:imgRed forState:UIControlStateNormal];
            [btnPurple setImage:imgPurple forState:UIControlStateNormal];
            [btnYellow setImage:imgYellow forState:UIControlStateNormal];
            [btnShowBarColor setImage:imgBar forState:UIControlStateNormal];
        }
            break;
        case 7: { //for lightgray
            tmpColor = [UIColor colorWithRed:148.0/255 green:148.0/255 blue:148.0/255 alpha:1.0];
            [btnWhite setImage:imgWhite forState:UIControlStateNormal];
            [btnGreen setImage:imgGreen1 forState:UIControlStateNormal];
            [btnGreenBold setImage:imgGreen2 forState:UIControlStateNormal];
            [btnLighGray setImage:imgLightgray_sel forState:UIControlStateNormal];
            [btnBlack setImage:imgBlack forState:UIControlStateNormal];
            [btnRed setImage:imgRed forState:UIControlStateNormal];
            [btnPurple setImage:imgPurple forState:UIControlStateNormal];
            [btnYellow setImage:imgYellow forState:UIControlStateNormal];
            [btnShowBarColor setImage:imgBar forState:UIControlStateNormal];
        }
            break;
        case 8: { //for black
            tmpColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:1.0];
            [btnWhite setImage:imgWhite forState:UIControlStateNormal];
            [btnGreen setImage:imgGreen1 forState:UIControlStateNormal];
            [btnGreenBold setImage:imgGreen2 forState:UIControlStateNormal];
            [btnLighGray setImage:imgLightgray forState:UIControlStateNormal];
            [btnBlack setImage:imgBlack_sel forState:UIControlStateNormal];
            [btnRed setImage:imgRed forState:UIControlStateNormal];
            [btnPurple setImage:imgPurple forState:UIControlStateNormal];
            [btnYellow setImage:imgYellow forState:UIControlStateNormal];
            [btnShowBarColor setImage:imgBar forState:UIControlStateNormal];
        }
            break;
        case 9: { 
            [btnWhite setImage:imgWhite forState:UIControlStateNormal];
            [btnGreen setImage:imgGreen1 forState:UIControlStateNormal];
            [btnGreenBold setImage:imgGreen2 forState:UIControlStateNormal];
            [btnLighGray setImage:imgLightgray forState:UIControlStateNormal];
            [btnBlack setImage:imgBlack forState:UIControlStateNormal];
            [btnRed setImage:imgRed forState:UIControlStateNormal];
            [btnPurple setImage:imgPurple forState:UIControlStateNormal];
            [btnYellow setImage:imgYellow forState:UIControlStateNormal];
            [btnShowBarColor setImage:imgBar_sel forState:UIControlStateNormal];
        }
            break;
            
        default:
            break;
    }
    
    if (button.tag == 9) {
        if (delegate && [delegate respondsToSelector:@selector(showBarColor)]) {
            [delegate showBarColor];
        }
    } else {
        if (tmpColor) {
            if (delegate && [delegate respondsToSelector:@selector(didSeletedColorBoard:)]) {
                [delegate didSeletedColorBoard:tmpColor];
            }
        }
        
        if (delegate && [delegate respondsToSelector:@selector(hideBarColor)]) {
            [delegate hideBarColor];
        }
    }
}


- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    
}

- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    
}

- (void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    
}

@end
